package com.lu.statistics.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lu.statistics.common.config.ServerResponse;
import com.lu.statistics.entity.MtDishCategories;
import com.lu.statistics.entity.MtDishInfo;
import com.lu.statistics.entity.enums.ChoiceTypeEnum;
import com.lu.statistics.entity.front.request.SelectCommonRequest;
import com.lu.statistics.entity.front.request.SelectDishSetRequest;
import com.lu.statistics.entity.front.request.SelectDishSingleRequest;
import com.lu.statistics.entity.front.response.DishCategoriesResponse;
import com.lu.statistics.entity.front.response.DishIGroupResponse;
import com.lu.statistics.entity.front.response.DishInfoResponse;
import com.lu.statistics.service.DishCategoriesService;
import com.lu.statistics.service.DishInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(value = "商品模块", tags = {"商品模块"})
@RestController
@RequestMapping("/api/dish")
public class SuDishController {
    @Autowired
    private DishInfoService dishInfoService;
    @Autowired
    private DishCategoriesService dishCategoriesService;

    @ApiOperation(value = "菜品分组信息(菜系、酒水)", notes = "菜品分组信息(菜系、酒水)")
    @GetMapping(value = "/queryGroupDish")
    public ServerResponse<DishIGroupResponse> queryGroupDishInfo(@RequestBody SelectCommonRequest request) {
        MtDishInfo dishInfo = new MtDishInfo();
        dishInfo.setCategoriesId(0);
        dishInfo.setChoiceType(ChoiceTypeEnum.SINGLE_DISH.name());
        DishIGroupResponse dishIGroupResponse = dishInfoService.queryGroupDish(dishInfo, request.getCurrent(), request.getPageSize());
        String isAvailable = "LISTING";
        //查询菜单套餐列表
        IPage page = dishCategoriesService.queryDishDishCategories(isAvailable, request.getCurrent(), request.getPageSize());
        List<DishCategoriesResponse> newList = new ArrayList<>();
        for (Object item : page.getRecords()) {
            MtDishCategories dishCategories = (MtDishCategories) item;
            String url = "/api/file/previewPhoto?fileFullPath=" + dishCategories.getImgUrl();
            DishCategoriesResponse dishCategoriesResponse = new DishCategoriesResponse();
            BeanUtils.copyProperties(dishCategories, dishCategoriesResponse);
            dishCategoriesResponse.setImgUrl(url);
            newList.add(dishCategoriesResponse);
        }
        dishIGroupResponse.setCategorieList(newList);
        dishIGroupResponse.setCgTotal(page.getTotal());
        return ServerResponse.success(dishIGroupResponse);
    }

    @ApiOperation(value = "查询菜品套餐", notes = "查询菜品套餐")
    @GetMapping(value = "/queryDishSet")
//    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", required = true, name = "id", value = "分类id", dataType = "int")})
    public ServerResponse<DishCategoriesResponse> queryDishSet(@RequestBody SelectDishSetRequest request) {
        DishCategoriesResponse response = dishCategoriesService.queryDishCategoriesById(request.id, request.getCurrent(), request.getPageSize());
        return ServerResponse.success(response);
    }

    @ApiOperation(value = "查询菜品自选", notes = "查询菜品自选")
    @GetMapping(value = "/queryDishSingle")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", required = true, name = "id", value = "菜品id", dataType = "int")})
    public ServerResponse<DishInfoResponse> queryDishSingle(Integer id) {
        DishInfoResponse response = dishInfoService.queryDishById(id);
        return ServerResponse.success(response);
    }

}
